<script setup lang="ts">
import {reactive, ref} from 'vue';
import {ElMessage} from 'element-plus'
import {useAgentApi} from '/@/api/judicial/agent';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 定义变量
const ruleFormRef = ref(null);
const state = reactive({
	ruleForm: {
		data_total: 0,
		pay_amount: 0,
	},
	dialog: {
		loading: false,
		isShowDialog: false,
		title: '',
		submitTxt: '',
	}
});

const rules = reactive ({
	data_total: [
		{ required: true, message: '请填写充值条数', trigger: 'blur',
			validator: (rule: any, value: any, callback: any) => {
				if (value <= 0) {
					callback(new Error('请填写充值条数'));
				} else {
					callback();
				}
			}
		},
	],
	pay_amount: [
		{ required: true, message: '请填写充值金额', trigger: 'blur',
			validator: (rule: any, value: any, callback: any) => {
				if (value <= 0) {
					callback(new Error('请填写充值金额'));
				} else {
					callback();
				}
			}
		},
	],
});

const submitForm = () => {
	ruleFormRef.value.validate((valid, fields) => {
		if (valid) {
			useAgentApi().agentRechargeSubmit(state.ruleForm).then((res) => {
				if (res.code == 200) {
					ElMessage.success(res.message);
					closeDialog();
				} else {
					ElMessage.error(res.message);
				}
			})
		} else {
			console.log('error submit!', fields)
		}
	})
};

// 打开弹窗
const openDialog = (title, row) => {
	state.dialog.title = title;
	state.dialog.submitTxt = '确认充值';
	state.ruleForm.id = row.id
	state.dialog.isShowDialog = true;
};

// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
	ruleFormRef.value.resetFields();
	emit('refresh');
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>

<template>
	<el-dialog @close="closeDialog" :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="400px">
		<el-form ref="ruleFormRef" :model="state.ruleForm" :rules="rules" size="default" status-icon class="demo-ruleForm">
			<el-row :gutter="35">
				<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
					<el-form-item label="充值条数" prop="data_total">
						<el-input type="number" v-model="state.ruleForm.data_total" size="default"></el-input>
					</el-form-item>
					<el-form-item label="充值金额" prop="pay_amount">
						<el-input type="number" v-model="state.ruleForm.pay_amount" size="default"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
				<span class="dialog-footer">
					<el-button type="primary" @click="submitForm" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
		</template>
	</el-dialog>
</template>

<style scoped lang="scss">

</style>